import 'package:flutter/material.dart';

main() => runApp(MyApp());

GlobalObjectKey<_Child1PageState> key =
    GlobalObjectKey<_Child1PageState>("1111");

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      title: "父子组件通讯",
      home: Scaffold(
        appBar: AppBar(
          title: Text("父子组件通讯"),
        ),
        body: Column(children: <Widget>[
          Child1Page(
            key: key,
          ),
          Child2Page()
        ]),
        floatingActionButton: FloatingActionButton(
          onPressed: () => {},
          child: Icon(
            Icons.refresh,
            color: Colors.red,
          ),
        ),
      ),
    );
  }
}

class Child1Page extends StatefulWidget {
  Child1Page({key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _Child1PageState();
  }
}

class _Child1PageState extends State<Child1Page> {
  String info = "child1";

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Text(info);
  }

  void updateMessage(value) {
    setState(() {
      info = value;
    });
  }
}

class Child2Page extends StatefulWidget {
  Child2Page({key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _Child2PageState();
  }
}

class _Child2PageState extends State<Child2Page> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return RaisedButton(
      child: Text("child2"),
      onPressed: () => {key.currentState.updateMessage(key.value)},
    );
  }
}

class stful extends StatefulWidget {
  @override
  _stfulState createState() => _stfulState();
}

class _stfulState extends State<stful> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

class stanimatar extends StatefulWidget {
  @override
  _stanimatarState createState() => _stanimatarState();
}

class inhertWidget extends InheritedWidget {
  const inhertWidget({
    Key key,
    @required Widget child,
  })  : assert(child != null),
        super(key: key, child: child);

  static inhertWidget of(BuildContext context) {
    return context.inheritFromWidgetOfExactType(inhertWidget) as inhertWidget;
  }

  @override
  bool updateShouldNotify(inhertWidget old) {
    return true;
  }
}

class _stanimatarState extends State<stanimatar>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;

  @override
  void initState() {
    _controller = AnimationController(vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

class aa extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
